﻿using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.Repository
{
    public class TechnologyRepository : RepositoryBase, ITechnologyRepository
    {
        public TechnologyRepository(WmsDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<int> ChangeCellListTechnology(List<string> trayBarcodeList, string technologyRouteCode, string technologyProcessCode, UserInfo userInfo)
        {
            return await dbContext.Database.ExecuteSqlRawAsync(@"UPDATE B_TRAY T
   SET T.TECHNOLOGY_ROUTE_CODE   = :TECHNOLOGY_ROUTE_CODE,
       T.TECHNOLOGY_PROCESS_CODE = :TECHNOLOGY_PROCESS_CODE,
       T.MODIFY_DATE             = :MODIFY_DATE,
       T.MODIFIED_BY             = :MODIFIED_BY,
       T.MODIFY_IP               = :MODIFY_IP
 WHERE T.Barcode IN (:Barcode)", new List<OracleParameter>()
            {
                new OracleParameter("TECHNOLOGY_ROUTE_CODE",technologyRouteCode),
                new OracleParameter("TECHNOLOGY_PROCESS_CODE",technologyProcessCode),
                new OracleParameter("MODIFY_DATE",DateTime.Now),
                new OracleParameter("MODIFIED_BY",userInfo.UserName),
                new OracleParameter("MODIFY_IP",userInfo.UserIp),
                new OracleParameter("Barcode",string.Join("','",trayBarcodeList))
            });
        }

        public async Task<int> ChangeTrayListTechnology(List<string> trayBarcodeList, string technologyRouteCode, string technologyProcessCode, UserInfo userInfo)
        {
            return await dbContext.Database.ExecuteSqlRawAsync(@"UPDATE B_CELL T
   SET T.TECHNOLOGY_ROUTE_CODE   = :TECHNOLOGY_ROUTE_CODE,
       T.TECHNOLOGY_PROCESS_CODE = :TECHNOLOGY_PROCESS_CODE,
       T.MODIFY_DATE             = :MODIFY_DATE,
       T.MODIFIED_BY             = :MODIFIED_BY,
       T.MODIFY_IP               = :MODIFY_IP
 WHERE T.TRAY_BARCODE IN (:TRAY_BARCODE)", new List<OracleParameter>()
            {
                new OracleParameter("TECHNOLOGY_ROUTE_CODE",technologyRouteCode),
                new OracleParameter("TECHNOLOGY_PROCESS_CODE",technologyProcessCode),
                new OracleParameter("MODIFY_DATE",DateTime.Now),
                new OracleParameter("MODIFIED_BY",userInfo.UserName),
                new OracleParameter("MODIFY_IP",userInfo.UserIp),
                new OracleParameter("TRAY_BARCODE",string.Join("','",trayBarcodeList))
            });
        }

        public async Task<List<TechnologyRoutePO>> GetAllTechnologyRouteList()
        {
            return await dbContext.Set<TechnologyRoutePO>().AsNoTrackingWithIdentityResolution().OrderBy(entity => entity.SortNumber).ToListAsync();
        }

        public async Task<int> GetMaxTechnologyRouteSortNumber()
        {
            return await dbContext.Set<TechnologyRoutePO>().MaxAsync(entity => (int?)entity.SortNumber) ?? 0;
        }

        public async Task<TechnologyRoutePO?> GetTechnologyRoute(string technologyRouteCode)
        {
            return await dbContext.Set<TechnologyRoutePO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.Code == technologyRouteCode);
        }

        public async Task<TechnologyProcessPO?> GetTechnologyProcess(string technologyProcessCode)
        {
            return await dbContext.Set<TechnologyProcessPO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.Code == technologyProcessCode);
        }

        public async Task<TechnologyRouteProcessPO?> GetTechnologyRouteProcess(string technologyRouteCode, string technologyProcessCode)
        {
            return await dbContext.Set<TechnologyRouteProcessPO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.TechnologyRouteCode == technologyRouteCode && entity.TechnologyProcessCode == technologyProcessCode);
        }

        public async Task<List<TechnologyRouteProcessPO>> GetTechnologyRouteProcessList(string technologyRouteCode)
        {
            return await dbContext.Set<TechnologyRouteProcessPO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.TechnologyRouteCode == technologyRouteCode).OrderBy(entity => entity.SortNumber).ToListAsync();
        }

        public async Task<LineTechnologyRoutePO?> GetLineStaionBindTechnologyRoute(string lineCode, string stationCode)
        {
            return await dbContext.Set<LineTechnologyRoutePO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.LineCode == lineCode && entity.StationCode == stationCode);
        }

        public async Task<LineTechnologyProcessRoutePO?> GetLineTechnologyProcessRoute(string lineCode, string technologyProcessCode)
        {
            return await dbContext.Set<LineTechnologyProcessRoutePO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.LineCode == lineCode && entity.TechnologyProcessCode == technologyProcessCode);
        }
    }
}
